import { Injectable }  from '@angular/core';
import { Observable }  from 'rxjs/Observable';
import { HttpService } from './http.service';


@Injectable()
export class GroupService {

  constructor(private http: HttpService) { }

  getGroups(): Observable<any> {
    let url = `/product_groups/`;
    console.log(`current request url: `, url);
    return this.http.get(url)
      .map(this.extractData)
      .catch(this.handlerError);
  }

  getGroupInfo(group_id: number): Observable<any> {
    let url = `/product-group-info/${group_id}`;
    console.log(`current request url ===> ${url}`);
    return this.http.get(url)
      .map(this.extractData)
      .catch(this.handlerError);
  }

  getGroupLists() {
    let url = `/groups_list/`;
    console.log('current request url : ', url);
    return this.http.get(url)
       .map(this.extractData)
       .catch(this.handlerError);
  }

  getGroupsPaginate(start: number, end: number) {
    let url = `/groups_list?_start=${start}&_end=${end}`;
    console.log('current request url : ', url);
    return this.http.get(url)
       .map(this.extractData)
       .catch(this.handlerError)
  }

  getGroupNote() {
    let url = `/group_notes/`;
    console.log(`current request url : `, url);
    return this.http.get(url)
       .map(this.extractData)
       .catch(this.handlerError);
  }

  addGroup(data: any) {
    let url = `/groups_list/${data}`;
    console.log('current request url : ', url);
    return this.http.post(url, data)
       .map(this.extractData)
       .catch(this.handlerError);
  }

  addGroupNote(data: any[]) {
    let url = `/group_notes/${data}`;
    console.log('current request url : ', url);
    return this.http.post(url, data)
       .map(this.extractData)
       .catch(this.handlerError);
  }

  deleteProductGroups(data: any) {
    let url = `/groups_list/${data}`;
    console.log('current request url : ', url);
    return this.http.put(url, data)
       .map((response: Response) => response.json())
       .catch(this.handlerError);
  }

  deleteGroupsNote() {
    let url = `/group_notes/`;
    console.log('current request url : ', url);
    return this.http.delete(url)
       .map((response: Response) => response.json())
       .catch(this.handlerError);
  }

  public DeleteGroupNote(id: any): Observable<any> {
    let url = `/group_notes/${id}`;
    console.log(`current request url : `, url);
    return this.http.delete(url)
       .map(this.extractData)
       .catch(this.handlerError)
  }

  public DeleteGroup() {
    let url = `/groups_list/`;
    console.log('current request url : ', url)
    return this.http.delete(url)
       .map(this.extractData)
       .catch(this.handlerError)
  }

  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }

  private handlerError(error: any) {
    let errMsg = (error.message) ? error.message:
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg);
        return Observable.throw(errMsg);
  }

}
